/*

 * Copyright (C) 2021 Huawei Device Co., Ltd.

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *     http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */

package zaizai.com.slice;

import com.ajguan.ResourceTable;
import com.ajguan.library.EasyRefreshLayout;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.ListContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.AttrHelper;
import ohos.app.Context;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

import zaizai.com.SimpleAdapter;
import zaizai.com.Toast;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 类似Fraction的界面
 */
public class EasyFraction extends DirectionalLayout implements Component.BindStateChangedListener {
    private static final String TAG = EasyFraction.class.getSimpleName();
    private EasyRefreshLayout easyRefreshLayout;
    private ListContainer listContainer;
    private SimpleAdapter adapter;
    private EventHandler eventHandler;

    /**
     * constructor
     *
     * @param context 上下文
     */
    public EasyFraction(Context context) {
        this(context, null);
    }

    /**
     * constructor
     *
     * @param context 上下文
     * @param attrSet 属性
     */
    public EasyFraction(Context context, AttrSet attrSet) {
        this(context, attrSet, null);
    }

    /**
     * constructor
     *
     * @param context   上下文
     * @param attrSet   属性
     * @param styleName 样式
     */
    public EasyFraction(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        Component parse = LayoutScatter.getInstance(context)
                .parse(ResourceTable.Layout_fraction_easy, this, true);
        initView(parse);
        initData();
        initListener();
        setBindStateChangedListener(this);
    }

    /**
     * 设置监听
     */
    private void initListener() {
        easyRefreshLayout.addEasyEvent(new EasyRefreshLayout.EasyEvent() {
            @Override
            public void onLoadMore() {
                loadMore();
            }

            @Override
            public void onRefreshing() {
                onFresh();
            }
        });
    }

    /**
     * 刷新数据
     */
    private void onFresh() {
        eventHandler.postTask(() -> {
            List<String> list = new ArrayList<>();
            for (int i = 0; i < 20; i++) {
                list.add("this is refresh data >>>" + new Date().toLocaleString());
            }
            adapter.setNewData(list);
            easyRefreshLayout.refreshComplete();
            Toast.showToast(getContext(), "refresh success");
        }, 1000);

    }

    /**
     * 加载更多数据
     */
    private void loadMore() {
        final List<String> list = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            list.add("this is  new load data >>>>" + new Date().toLocaleString());
        }
        eventHandler.postTask(() -> {
            easyRefreshLayout.closeLoadView();
            adapter.getData().addAll(list);
            adapter.notifyDataChanged();
            listContainer.scrollBy(0, AttrHelper.fp2px(60, getContext()));
        }, 500);
    }

    /**
     * 模拟数据
     */
    private void initData() {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            list.add("EasyRefreshLayout index :" + i);
        }
        adapter.getData().addAll(list);
        adapter.notifyDataChanged();
    }

    /**
     * 刷新列表数据
     */
    public void notifyDataChanged() {
        adapter.notifyDataChanged();
    }

    /**
     * 初始化
     *
     * @param parse rootview
     */
    private void initView(Component parse) {
        eventHandler = new EventHandler(EventRunner.getMainEventRunner());
        easyRefreshLayout = (EasyRefreshLayout) parse.findComponentById(ResourceTable.Id_easylayout);
        listContainer = (ListContainer) parse.findComponentById(ResourceTable.Id_list_container);
        adapter = new SimpleAdapter();
        listContainer.setItemProvider(adapter);
    }

    @Override
    public void onComponentBoundToWindow(Component component) {
        notifyDataChanged();
    }

    @Override
    public void onComponentUnboundFromWindow(Component component) {
    }
}
